const fs = require('fs')
const ShpConverter = require('../utils/shpConverter')

/**
 * geojson转shp测试
 * @param {String} outputPath 输出路径
 */
async function geojson2shpTest(outputPath) {
  // json文件转换为shp系列文件
  const inputJson = fs.readFileSync('./testdata/two_states.json')
  await ShpConverter.geojson2shp(inputJson, 'fromJson')
    .then(result => {
      console.log('geojson2shpTest success')
      // 遍历结果对象，把结果写入目录
      for (const filename in result) {
        fs.writeFileSync(`${outputPath}${filename}`, result[filename])
      }
    })
    .catch(err => {
      console.log('geojson2shpTest error')
      console.log(err)
    })
}
/**
 * shp系列文件压缩包转json
 * @param {String} outputPath 
 */
async function shpzip2geojsonTest(outputPath) {
  const zipFile = fs.readFileSync('./testdata/testdata.zip')
  await ShpConverter.shp2geojson({zipFile}, 'fromZip')
    .then(result => {
      console.log('shpzip2geojsonTest success')
      // 遍历结果对象，把结果写入目录
      for (const filename in result) {
        fs.writeFileSync(`${outputPath}${filename}`, result[filename])
      }
    })
    .catch(err => {
      console.log('shpzip2geojsonTest error')
      console.log(err)
    })
}
/**
 * shp系列文件转json测试
 * @param {String} outputPath 
 */
async function shp2geojsonTest(outputPath) {
  const shpFile = fs.readFileSync('./testdata/two_states.shp')
  const shxFile = fs.readFileSync('./testdata/two_states.shx')
  const dbfFile = fs.readFileSync('./testdata/two_states.dbf')
  const prjFile = fs.readFileSync('./testdata/two_states.prj')
  await ShpConverter.shp2geojson(
    {shpFile, shxFile, prjFile, dbfFile},
    'fromShps'
  )
    .then(result => {
      console.log('shp2geojsonTest success')
      // 遍历结果对象，把结果写入目录
      for (const filename in result) {
        fs.writeFileSync(`${outputPath}${filename}`, result[filename])
      }
    })
    .catch(err => {
      console.log('shp2geojsonTest error')
      console.log(err)
    })
}
module.exports = {
  geojson2shpTest,
  shp2geojsonTest,
  shpzip2geojsonTest,
}
